package com.sdgakj.service.code;

import com.google.common.collect.Maps;
import com.sdgakj.entity.Code;
import com.sdgakj.repository.CodeDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class CodeService {

	@Autowired
	private CodeDao codeDao;
	
	public Code getCodeById(Long id) {
		return codeDao.findOne(id);
	}
	
	
	public List<Code> getColorByName(String name) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (StringUtils.isNotBlank(name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<Code> spec = DynamicSpecifications.bySearchFilter(filters.values(), Code.class);
		return codeDao.findAll(spec);
	}
	
	
	
	public void updateCode(Code entity) {
		codeDao.save(entity);
	}
	
	
	public List<Code> getCodeList() {
		return (List<Code>) codeDao.findAll();
	}
	
	public List<Code> getCodeList(String sortName,String sortType) {
		Sort sort = buildSortRequest(sortName,sortType);
		return (List<Code>) codeDao.findAll(sort);
	}
	
	public void saveCode(Code entity) {
		codeDao.save(entity);
	}
	
	public void deleteCode(Long id) {
		Code tempCode = this.getCodeById(id);
		this.saveCode(tempCode);
	}
	
	
	public List<Code> getCodeAllByTag(String deltag,String sortName,String sortType) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (StringUtils.isNotBlank(deltag)) {
			filters.put("deltag", new SearchFilter("deltag", Operator.EQ, deltag));
		}
		Specification<Code> spec = DynamicSpecifications.bySearchFilter(filters.values(), Code.class);
		Sort sort = buildSortRequest(sortName,sortType);
		return codeDao.findAll(spec, sort);
	}

	public Page<Code> getCodeByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
			String sortType) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortName, sortType);
		Specification<Code> spec = buildSpecification(searchParams);
		return codeDao.findAll(spec, pageRequest);
	}
	 /**
		 * 获取所有的邀请码
		 * @param code
		 * @return
		 */
		public Map<String,Object> getSpreadByCode(){
			List<Code> codes = (List<Code>)codeDao.findAll();
			Map<String,Object> codeMap =new HashMap<String,Object>();
			for(Code s :codes){
				codeMap.put(s.getCode(), "1");
			}
			return codeMap;
		}	
	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort = new Sort(Direction.DESC, sortName);
		}else{
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize,  String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}
		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Code> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<Code> spec = DynamicSpecifications.bySearchFilter(filters.values(), Code.class);
		return spec;
	}
	
}
